from urllib.parse import urlencode


# 分页器
class Pagination(object):
    def __init__(self, current_page, total_count, base_url, params, per_page_count=10, max_pager_count=11):
        """"
        current_page: 当前页码
        total_count: 数据库中的总条数
        base_url: 请求的基础url
        params: 请求的额外参数
        per_page_count: 每页显示的数据条数
        max_pager_count: 页面上最多显示的页码数量
        """
        # 当前页码
        self.current_page = current_page
        # 参数
        self.params = params
        # url
        self.base_url = base_url
        # 数据总条数
        self.total_count = total_count

        # 每页显示10条数据
        self.per_page_count = per_page_count

        # 页面上应该显示的最大页码
        max_page_num, div = divmod(total_count, per_page_count)

        self.max_page_num = max_page_num
        self.max_pager_count = max_pager_count
        if div:
            max_page_num += 1

        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1

        if current_page < 0 or current_page > self.max_page_num:
            self.current_page = 1

        # 分页的中值
        self.half_pagr_count = int(self.max_pager_count / 2)
        if self.max_page_num < self.max_pager_count:
            # 如果可分页的页码小于最大页码，则显示所有页码
            self.max_pager_count = self.max_page_num

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page_count

    @property
    def end(self):
        return self.current_page * self.per_page_count

    def page_html(self):
        # 计算页码的开始和结束
        # 1 正常
        start = self.current_page - self.half_pagr_count
        end = self.current_page + self.half_pagr_count

        if self.current_page <= self.half_pagr_count:
            start = 1
            end = self.max_pager_count
        if self.current_page + self.half_pagr_count >= self.max_page_num:
            start = self.max_page_num - self.max_pager_count
            end = self.max_page_num
        # 生成分页
        page_list = []

        # 上一页
        if self.current_page != 1:
            self.params['page'] = self.current_page - 1
            page_list.append(f'<li> <a  href="{self.base_url}?{self.query_encode }">上一页</a> </li>')


        for i in range(start, end + 1):
            self.params['page'] = i
            if self.current_page == i:
                li = f'<li class="active"> <a href="{self.base_url}?{self.query_encode}">{i}</a> </li>'
            else:
                li = f'<li> <a href="{self.base_url}?{self.query_encode}">{i}</a> </li>'
            page_list.append(li)

        # 下一页
        if self.current_page != self.max_page_num:
            self.params['page'] = self.current_page + 1
            page_list.append(f'<li> <a href="{self.base_url}?{self.query_encode}">下一页</a> </li>')
        return ''.join(page_list)

    @property
    def query_encode(self):
        return urlencode(self.params)


if __name__ == '__main__':
    page = Pagination(current_page=1,
                      total_count=100,
                      base_url='/app01/comments/',
                      params={'a': 1, 'b': 2},
                      per_page_count=5,
                      max_pager_count=7
                      )
    print(page.page_html())

